import 'package:enterprise_v6_app/app/components/custom_refresh.dart';
import 'package:enterprise_v6_app/app/components/special_operation/custom_select_function.dart';
import 'package:enterprise_v6_app/app/core/constans/dict_data_info.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/http/http.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/data/entities/application/get_dict_all_entity.dart';
import 'package:enterprise_v6_app/app/data/entities/application/smart_warehouse/product_entity.dart';
import 'package:enterprise_v6_app/app/data/entities/application/smart_warehouse/warehouse_entity.dart';
import 'package:enterprise_v6_app/app/modules/application/smart_warehouse/smart_warehouse_controller.dart';
import 'package:enterprise_v6_app/router/app_pages.dart';

class ProductManagementController extends GetxController {
  /// 列表刷新key
  final GlobalKey<CustomEasyRefreshListState> globalKey = GlobalKey();

  /// 请求参数
  Map<String, dynamic> queryParameters = {};

  /// 请求参数-搜索字符串
  String? search;

  /// 请求参数-库存情况
  String inventoryType = '';

  /// 请求参数-仓库id
  String wareHouseId = '';

  /// 当前选择仓库
  WarehouseEntity warehouse = WarehouseEntity();

  /// 智慧仓库-库存情况类型
  List<GetDictAllEntity> dictInventoryType = [];

  /// 库存情况
  GetDictAllEntity inventoryEntity = GetDictAllEntity();

  @override
  void onInit() {
    super.onInit();
    initData();
  }

  Future<void> initData() async {
    dictInventoryType = await DictDataInfo.getDictionaryDataByTree(dictionaryType: '511410025ZCJjAZ3q');
  }

  /// 获取产品列表数据
  Future<List<ProductEntity>> getProductListData(
    int pageNum,
    int pageSize,
  ) async {
    queryParameters['pageNum'] = pageNum;
    queryParameters['pageSize'] = pageSize;
    queryParameters['wareHouseName'] = search;
    queryParameters['inventoryType'] = inventoryType;
    queryParameters['wareHouseId'] = wareHouseId;

    // 获取产品列表接口
    final res = await HttpUtil()
        .generatePostRequest<List<ProductEntity>>(
          Interface.getProductByPage,
          data: queryParameters,
        )
        .execute();
    List<ProductEntity>? data;
    res.onSuccess((result) {
      if (result.data != null) {
        data = result.data!;
      }
    });

    return data ?? [];
  }

  /// 所在仓库选择
  void showWarehouseSelection() {
    // 获取仓库列表
    final warehouseList = Get.find<SmartWarehouseController>().warehouseList;
    CustomSelectFunction.selectMultipleBottomSheet<WarehouseEntity>(
      title: '所在仓库',
      dataList: warehouseList,
      checkedList: [warehouse],
      isMultiple: false,
      onCallBack: (value) {
        warehouse = value[0];
        wareHouseId = warehouse.getId();
        refreshPage();
        update();
      },
    );
  }

  /// 库存情况选择
  void showInventorySelection() {
    CustomSelectFunction.selectMultipleBottomSheet<GetDictAllEntity>(
      title: '库存情况',
      dataList: dictInventoryType,
      checkedList: [inventoryEntity],
      isMultiple: false,
      onCallBack: (value) {
        inventoryEntity = value[0];
        inventoryType = inventoryEntity.getId();
        refreshPage();
        update();
      },
    );
  }

  /// 进入详情页面
  void toDetailsPage(String id) {
    Get.toNamed(AppRoutes.productDetails, arguments: id);
  }

  /// 搜索刷新页面
  void refreshPage() {
    globalKey.currentState?.onRefresh();
  }
}

/// todo 测试用产品类
class ProductTest {
  ProductTest(this.name, this.num);

  final String name;
  final String num;
}
